import { ATeamCombat } from "../TeamCombat";

export class RangeAttackStrategy
{
    private static attackAroundCreep(team:ATeamCombat)
    {
        const attack  = team.getAttacker();
        if(attack.length==0)
        {
            return false;
        }

        let findcreeps = team.findHostilesCreeps();
        findcreeps = _.sortBy(findcreeps,e=>e.getActiveBodyparts(TOUGH));
        // global.Log.Info(`攻击附近的爬虫单位 :${JSON.stringify(_.map(findcreeps,e=>e.name))}`);
        let ret = false;
        for(const e of attack)
        {
            if(this.attackCreeps(e,findcreeps))
            {
                ret = true;
            }
        }
        return ret;
    }
    
    private static attackAroundStructures(team:ATeamCombat)
    {

        const attack  = team.getAttacker();
        if(attack.length>0)
        {
            const cachestructure=  team.room.getWorld().PollStructure(attack[0].pos.roomName);
            if(cachestructure)
            {
                const structures = cachestructure.findInRange(attack[0].pos,3).filter(e=>e.structureType!== STRUCTURE_ROAD  && e.structureType!== STRUCTURE_CONTAINER);;

                if(structures.length==0)
                {
                    return false;
                }
                for(const atk of attack)
                {
                    for(const e of structures)
                    {
                        const distance = atk.pos.distance(e.pos);
                        if(distance<=3 && !e.room.IsMy())
                        {
                            if(distance>1)
                            {
                                atk.rangedAttack(e);
                            }
                            else
                            {
                                atk.rangedMassAttack();
                            }
                            break;
                        }
                    }

                }
                return true;
            }
        }
        return false;
    }
     /**
     * 全都在rampart外
     * @param attacker
     * @param targets
     * @returns
     */
    
    private static attackCreeps(attacker:Creep,targets:Creep[])
    {
        let scale=0;

        const inrange:Creep[]=[];
        for(const e of targets)
        {
            const distance = e.pos.distance(attacker.pos);
            switch(distance)
            {
                case 1:
                    scale+=1;
                    inrange.push(e);
                    break;
                case 2:
                    scale+=0.4;
                    inrange.push(e);
                    break;
                case 3:
                    scale+=0.1;
                    inrange.push(e);
                    break;
            }
        }
        if(inrange.length==0)
        {
            return false;
        }
        if(scale>=1)
        {
            attacker.rangedMassAttack();
        }
        else
        {
            attacker.rangedAttack(targets[0]);
        }
        return true;
    }
    
    private static attackTargets(team:ATeamCombat,targets:string[])
    {
        if(targets.length>0)
        {


            const attacker = team.getAttacker();
            for(const item of attacker)
            {
                const obj = Game.getObjectById(targets[0] as Id<Creep>) as AnyCreep | Structure<StructureConstant>;
                const distance = obj.pos.distance(item.pos);
                if(distance>1 && distance<=3)
                {
                    item.rangedAttack(obj);
                }
                else
                {
                    if(distance<=1)
                    {
                        if((obj as Structure<StructureConstant>).structureType == STRUCTURE_WALL)
                        {
                            item.rangedAttack(obj);
                        }
                        else
                        {
                            item.rangedMassAttack();
                        }
                    }
                    else
                    {
                        item.rangedAttack(obj);
                    }
                }
            }
        }
        else
        {
            const anchor = team.getAnchor();
            if(!anchor)
            {
                return
            }
            const cache = team.room.getWorld().PollEvents(anchor.roomName);
            if(cache )
            {
                const events = cache.inquireEvent(EVENT_ATTACK);
                if(events)
                {
                    const find = _.find(events,e=>

                        Game.getObjectById(e.data.targetId as (Id<OwnedStructure|Creep>))?.my)
                    if(find)
                    {
                        for(const e of team.creeps)
                        {
                            e.rangedMassAttack();
                        }
                    }
                }

            }
        }
    }
    /**
     * 从target中选取歼灭目标，和骚扰目标进行攻击
 */
    
    public static attack(team:ATeamCombat,targets:string[])
    {
        if(this.attackAroundCreep(team))
        {
            return ;
        }
        if(this.attackAroundStructures(team))
        {
            return ;
        }
        this.attackTargets(team,targets);
    }
}